package com.alon.spring01.study;


import com.alon.spring01.study.refect.RefectService;
import com.esotericsoftware.reflectasm.MethodAccess;

import java.lang.reflect.Method;

/**
 * @program: spring01
 * @description: 反射工具包测试
 * @author: yulongguang
 * @create: 2019-08-29 20:19
 **/
public class ReflectTest {
    public static void main(String[] args) {
        ReflectTest reflectTest = new ReflectTest();
        reflectTest.testJdkReflect();
        reflectTest.testReflectAsm4Name();
    }

    /**
     * 用jdk调用方法
     */
    public void testJdkReflect() {
        RefectService target = new RefectService();
        Method method = null;
        try {
            method = target.getClass().getMethod("update", int.class, String.class);
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) {
                method.invoke(target, 1, "zahngsan");
            }
            long end = System.currentTimeMillis();
            System.out.println("timeout=" + (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 用reflectasm反射获取方法
     */
    public void testReflectAsm4Name() {
        RefectService target = new RefectService();
        MethodAccess access = MethodAccess.get(RefectService.class);//生成字节码的方式创建UserServiceMethodAccess
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            access.invoke(target, "update", 1, "zhangsan");
        }
        long end = System.currentTimeMillis();
        System.out.println("timeout=" + (end - start));//523 382 415 489 482
    }
    /**
     * 用reflectasm 发射获取方法
     * 用方法和字段索引定位反射方法，
     */
    public void testReflectAsm5Name(){
        RefectService target=new RefectService();
    }
}
